/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package login;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
 * This class is used to coordinate the process of taking in data from
 * the form on register.jsp  Then, attempting to add a new user to
 * the database with RegisterDAO.  Finally, taking the result of RegisterDAO
 * and displaying the appropriate screen to the user.
 * @author Ben
 */
public class RegisterServlet  extends HttpServlet {
        
        /**
         * The constructor for the RegisterServlet
         */
        public RegisterServlet() {
        super();
    }

	/**
	 * method called for GET action
         * 
         * @param request 
         * @param response 
         * @throws ServletException
         * @throws IOException  
         */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	}

	/**
	 * method called for POST action in register.jsp
         * It will use the information entered and store it in userAttempt.
         * userAttempt is passed around through RegisterDAO where all the
         * checks are for the different datafields.
         * 
         * If the user fails to follow the form's format they are sent back to
         * the register.jsp with a message of the first thing they did wrong.
         * 
         * If the user correctly fills out the form, then the respected info is 
         * added in a new row of the database.
         * 
         * @param request 
         * @param response
         * @throws ServletException
         * @throws IOException  
         */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try
		{
			User user = new User(request.getParameter("uname"), request.getParameter("password"),
                                request.getParameter("firstName"), request.getParameter("lastName"), request.getParameter("email"),
                                request.getParameter("climateZone"), request.getParameter("street1"),
                                request.getParameter("street2"), request.getParameter("city"),
                                request.getParameter("state"), request.getParameter("zip"), request.getParameter("interests"));
			user = RegisterDAO.register(user);
			if(user.isValid() && user.getRegistrationStatus()=="Successfully Registered")
			{
                                user.setUsername(user.getUsername().replaceAll("''", "'"));
                                user.setPassword(user.getPassword().replaceAll("''", "'"));
                                user.setFirstName(user.getFirstName().replaceAll("''", "'"));
                                user.setLastName(user.getLastName().replaceAll("''", "'"));
                                HttpSession session = request.getSession(true);
				session.setAttribute("currentSessionUser",user);
				response.sendRedirect("home.jsp?registerAttempt=success");
			}else{
                            response.sendRedirect("register.jsp?failReport="+user.getRegistrationStatus());
                        }
		} catch (Throwable exc)

		{ 
			System.out.println(exc);
		}
	}

}
